<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="课程id" prop="courseId">
      <el-input v-model="dataForm.courseId" readonly placeholder="课程id"></el-input>
    </el-form-item>
      <el-form-item label="课程id" prop="courseId">
        <el-input v-model="dataForm.courseName" readonly placeholder="课程id"></el-input>
      </el-form-item>
    <el-form-item label="学号" prop="courseId">
      <el-input v-model="dataForm.courseId" readonly placeholder="课程id"></el-input>
    </el-form-item>
    <el-form-item label="成绩" prop="scores">
      <el-input v-model="dataForm.scores" :readonly="dataForm.id?true:false" placeholder="成绩"></el-input>
    </el-form-item>
    <el-form-item label="学分绩点" prop="credits">
      <el-input v-model="dataForm.credits" :readonly="dataForm.id?true:false" placeholder="学分绩点"></el-input>
    </el-form-item>
    <el-form-item label="课时" prop="classHour">
      <el-input v-model="dataForm.classHour" :readonly="dataForm.id?true:false" placeholder="课时"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button :disabled="!!dataForm.id" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {courseApi, scoresApi} from '@/api/course'

  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: null,
          courseId: '',
          scores: '',
          courseName:'',
          credits: '',
          classHour: '',
          studentId:null
        },
        dataRule: {
          courseId: [
            { required: true, message: '课程id不能为空', trigger: 'blur' }
          ],
          scores: [
            { required: true, message: '成绩不能为空', trigger: 'blur' }
          ],
          credits: [
            { required: true, message: '学分绩点不能为空', trigger: 'blur' }
          ],
          classHour: [
            { required: true, message: '课时不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (sid,courseId) {
        this.dataForm = this.$options.data().dataForm
        this.dataForm.studentId = sid
        this.dataForm.courseId = courseId
        console.log(sid,courseId)
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })

        scoresApi.scoresInfo({studentId:sid,courseId:courseId})
        .then(({scores})=>{
          if(scores){
            this.dataForm = scores
          }
          return courseApi.courseInfo(courseId)

        }).then(({course})=>{
          this.dataForm.courseName = course.courseName
        })

      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            scoresApi.saveScores(this.dataForm)
            .then(()=>{
              this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
            })

          }
        })
      }
    }
  }
</script>
